package com.fan.springboot.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fan.springboot.domain.Song;
import com.fan.springboot.service.SongService;
import com.fan.springboot.utils.ResultVOUtil;

@RestController
@RequestMapping("/api/song")
public class TestController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SongService songService;

	@GetMapping("/hello")
	public String hello() {
		return "hello world!";
	}

	@GetMapping("/list")
	public Object list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, Map<String, Object> map) {
		PageRequest request = new PageRequest(page - 1, size);
		Page<Song> songList = songService.findAll(request);
		map.put("songList", songList);
		map.put("currentPage", page);
		map.put("size", size);

		return ResultVOUtil.success(map);
	}

	// 添加或修改
	@PostMapping("/addOrUpdate")
	public Object addOrUpdate(@RequestBody Song song) {
		logger.info("song: {}" + song);
		songService.save(song);
		return ResultVOUtil.success();
	}

	// 删除
	@PostMapping("/delete")
	public Object delete(@RequestParam("id") Integer id) {
		songService.delete(id);
		return ResultVOUtil.success();
	}

	// 根据id获取详细
	@GetMapping("/getDetail")
	public Object getDetail(@RequestParam("id") Integer id) {
		Song song = songService.findOne(id);
		return ResultVOUtil.success(song);
	}

}
